Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Containers

  • To run a container with its default command, use: docker run <image-name> <command>
  • To list containers, use: docker ps
  • To copy a file from/into a running container, use: docker cp <file-1> <file-2>
  • To execute a command in a running container, use: docker exec <container-id> <command>
  • To remove container, use: docker rm <container-id>
Info
titleFlags

Each command supports additional flags passed along the main arguments. Please make sure to check docker help <command> for more information.

The most often used flags are:

  • --name for docker run to specify friendly name for the container
  • -i, --interactive and -t, --tty for docker run and docker exec when you want to work in an interactive shell inside of the container

Exercises

Checking /etc/os-release of different containers

  1. (Almost) Every Linux OS has a file named /etc/os-release with some information about the platform
  2. To see that the containers have indeed their own isolated environments, check the output of docker run <image-name> cat /etc/os-release for a few different image names: ubuntu, debian, postgres, mysql, alpine
  3. In Docker Hub, each image is actually a combination of name and tag separated by colon. Check again the contents of /etc/os-release for: ubuntu:xenial, ubuntu:bionic, centos:7, centos:8

Working interactively in a container

  1. Run a new container with an interactive session: docker run -it ubuntu /bin/bash
  2. You are now logged as root, update APT cache: apt update
  3. Install fortune and cowsay: apt install -y fortune cowsay
  4. Run a few times: /usr/games/cowsay $(/usr/games/fortune)

Running a service in a container

  1. Create a new directory on your computer: mkdir /tmp/docker-exercise
  2. Add some HTML content, for example: echo '<img src="https://picsum.photos/200"/>' > /tmp/docker-exercise/index.html
  3. Run a container with --publish flag to forward network traffic from host's 8080 port to container's 80 port: docker run --volume /tmp/docker-exercise/:/usr/local/apache2/htdocs/ --publish 8080:80 httpd:2.4
  4. Open in a web browser: http://localhost:8080
Info
titleAbsolute path

Note, that flag --volume of docker run requires that given paths are absolute, not relative.


IMAS image