SE-Foundation Help

Docker

What is Docker and why is it popular?

Take note: The following instructions are run in a ubuntu-xenial virtual machine setup using Vagrant. To do the same, you can also install docker in any Vagrant virtual machine, or install docker on your host OS (Windows, Linux or Mac OS)

Let’s first pull a Docker image and run a container:

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker run -d -ti ubuntu:16.04
Unable to find image 'ubuntu:16.04' locally 16.04: Pulling from library/ubuntu 34667c7e4631: Pull complete d18d76a881a4: Pull complete 119c7358fbfc: Pull complete 2aaf13f3eff0: Pull complete Digest: sha256:58d0da8bc2f434983c6ca4713b08be00ff5586eb5cdff47bcde4b2e88fd40f88 Status: Downloaded newer image for ubuntu:16.04 e1fc0d4bbb5d3513b8f7666c91932812da7640346f6e05b7cfc3130ddbbb8278
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1fc0d4bbb5d ubuntu:16.04 "/bin/bash" About a minute ago Up About a minute keen_blackwell

Note that docker command will pull the Ubuntu docker container image from the Internet and run it. I let you look at the meaning of the flags using the command docker run --help, the main idea is that it keeps the container up and running.

To execute a command on the Docker container, use docker exec:

docker exec -i e1fc0d4bbb5d hostname
e1fc0d4bbb5d
hostname
ubuntu-xenial

If you want to connect to your Docker container and use Bash, you need to use docker exec -ti:

docker exec -ti e1fc0d4bbb5d /bin/bash
root@e1fc0d4bbb5d:/# echo "I am in $(hostname) Docker container" I am in e1fc0d4bbb5d Docker container root@e1fc0d4bbb5d:/# exit exit

If you want to stop a container, use docker stop:

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e1fc0d4bbb5d ubuntu:16.04 "/bin/bash" 5 minutes ago Up 5 minutes keen_blackwell
docker stop e1fc0d4bbb5d
e1fc0d4bbb5d
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Last modified: 05 September 2024