...
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 | ||
---|---|---|
| ||
Each command supports additional flags passed along the main arguments. Please make sure to check The most often used flags are:
|
Exercises
Checking /etc/os-release of different containers
- (Almost) Every Linux OS has a file named
/etc/os-release
with some information about the platform - 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
- 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
- Run a new container with an interactive session:
docker run -it ubuntu /bin/bash
- You are now logged as root, update APT cache:
apt update
- Install
fortune
andcowsay
:apt install -y fortune cowsay
- Run a few times:
/usr/games/cowsay $(/usr/games/fortune)
Running a service in a container
- Create a new directory on your computer:
mkdir /tmp/docker-exercise
- Add some HTML content, for example:
echo '<img src="https://picsum.photos/200"/>' > /tmp/docker-exercise/index.html
- 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
- Open in a web browser: http://localhost:8080
Info | ||
---|---|---|
| ||
Note, that flag --volume of docker run requires that given paths are absolute, not relative. |