Tutorial by Examples

docker run hello-world This will fetch the latest hello-world image from the Docker Hub (if you don't already have it), create a new container, and run it. You should see a message stating that your installation appears to be working correctly.
docker run docker/whalesay cowsay 'Hello, StackExchange!' This command tells Docker to create a container from the docker/whalesay image and run the command cowsay 'Hello, StackExchange!' in it. It should print a picture of a whale saying Hello, StackExchange! to your terminal. If the entrypoin...
Normally, a Docker container persists after it has exited. This allows you to run the container again, inspect its filesystem, and so on. However, sometimes you want to run a container and delete it immediately after it exits. For example to execute a command or show a file from the filesystem. Dock...
By default, containers created with docker run are given a random name like small_roentgen or modest_dubinsky. These names aren't particularly helpful in identifying the purpose of a container. It is possible to supply a name for the container by passing the --name command line option: docker run -...
docker run -p "8080:8080" myApp docker run -p "192.168.1.12:80:80" nginx docker run -P myApp In order to use ports on the host have been exposed in an image (via the EXPOSE Dockerfile directive, or --expose command line option for docker run), those ports need to be bound to...
docker run --restart=always -d <container> By default, Docker will not restart containers when the Docker daemon restarts, for example after a host system reboot. Docker provides a restart policy for your containers by supplying the --restart command line option. Supplying --restart=always ...
To keep a container running in the background, supply the -d command line option during container startup: docker run -d busybox top The option -d runs the container in detached mode. It is also equivalent to -d=true. A container in detached mode cannot be removed automatically when it stops, t...
A Docker volume is a file or directory which persists beyond the lifetime of the container. It is possible to mount a host file or directory into a container as a volume (bypassing the UnionFS). Add a volume with the -v command line option: docker run -d -v "/data" awesome/app bootstrap....
$ docker run -e "ENV_VAR=foo" ubuntu /bin/bash Both -e and --env can be used to define environment variables inside of a container. It is possible to supply many environment variables using a text file: $ docker run --env-file ./env.list ubuntu /bin/bash Example environment variable...
By default, containers created with docker run are given a random hostname. You can give the container a different hostname by passing the --hostname flag: docker run --hostname redbox -d ubuntu:14.04
To run a container interactively, pass in the -it options: $ docker run -it ubuntu:14.04 bash root@8ef2356d919a:/# echo hi hi root@8ef2356d919a:/# -i keeps STDIN open, while -t allocates a pseudo-TTY.
Set memory limit and disable swap limit docker run -it -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash Set both memory and swap limit. In this case, container can use 300M memory and 700M swap. docker run -it -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash
Log into a running container A user can enter a running container in a new interactive bash shell with exec command. Say a container is called jovial_morse then you can get an interactive, pseudo-TTY bash shell by running: docker exec -it jovial_morse bash Log into a running container with a s...
In cases such as restoring a database dump, or otherwise wishing to push some information through a pipe from the host, you can use the -i flag as an argument to docker run or docker exec. E.g., assuming you want to put to a containerized mariadb client a database dump that you have on the host, in...
While attached to a running container with a pty assigned (docker run -it ...), you can press ControlP - ControlQ to detach.
docker run --name="test-app" --entrypoint="/bin/bash" example-app This command will override the ENTRYPOINT directive of the example-app image when the container test-app is created. The CMD directive of the image will remain unchanged unless otherwise specified: docker run -...
docker run --add-host="app-backend:10.15.1.24" awesome-app This command adds an entry to the container's /etc/hosts file, which follows the format --add-host <name>:<address>. In this example, the name app-backend will resolve to 10.15.1.24. This is particularly useful for t...
A container will stop if no command is running on the foreground. Using the -t option will keep the container from stopping, even when detached with the -d option. docker run -t -d debian bash
docker stop mynginx Additionally, the container id can also be used to stop the container instead of its name. This will stop a running container by sending the SIGTERM signal and then the SIGKILL signal if necessary. Further, the kill command can be used to immediately send a SIGKILL or any ot...
When required you can tell Docker to execute additional commands on an already running container using the exec command. You need the container's ID which you can get with docker ps. docker exec 294fbc4c24b3 echo "Hello World" You can attach an interactive shell if you use the -it option...

Page 1 of 2