Docker
Containers
Basics
- Run container (e.g. nginx)
docker run nginx
- Run container with specific version (tag)
docker run <container_name>:<tag>
docker run redis:4.0
- List containers
- Running
docker ps
- All
docker ps -a
- Running
- Stop container
docker stop <container_id/name>
- Remove container
docker rm <container_id/name>
- Map Ports
- Map Docker Host port to Docker Container port
docker run -p <host_port>:<container_port> <container_id/name>
- E.g. Map port 80 of Docker Host to port 5000 of Docker Container
docker run -p 80:5000 my_container
- Map Docker Host port to Docker Container port
- Map Volumes
- Map Docker Host Volume to Docker Container Volume
docker run -v <host_path>:<container_path> <container_id/name>
docker run -v /opt/datadir:/var/lib/mysql mysql
- Map Docker Host Volume to Docker Container Volume
- Get more detailed info of container
docker inspect <container_id/name>
- Show Container logs
docker logs <container_id/name>
- Set Environmental Variables
docker run --env <variable>=<value> <container_id/name>
docker run --env JAVA_VERSION=11 my_container
--env
~-e
- Display Environmental Variables
- Use
docker inspect
command
- Use
Images
- Build docker image
docker build <path_of_Dockerfile>
docker build .
- Tag docker image you are building
docker build . -t <repository>/<image_name>
- Tag docker image you have built
docker tag <image> <repository>/<image_name>:<tag>
- Download image
docker pull <image_name>
- Show downloaded images
docker images
- Download image into computer
docker image save <image> > image.tar
- Show image history (some commands and sizes)
docker history <image>
- Remove image
docker rmi <image_name>
Commands
- Run container with command
docker run ubuntu <command>
docker run ubuntu sleep 5
- Run command on a running container
docker exec <container_id/name> <command>
docker exec my_ubuntu cat /etc/hosts
- Run container in detached mode
docker run -d <container_id/name>
- Attach to running container
docker attach <container_id/name>
- Enable interactive mode to make container receive input
docker run --interactive <container_id/name>
--interactive
~-i
- Attach to terminal (to see its output)
docker run --tty <container_id/name>
--tty
~-t
- Run container in combined mode to have both terminal input and output
docker run -it <container_id/name>
COMMAND
s and ENTRYPOINT
s
- Specifying Commands in Dockerfile
- Shell form
CMD <command> <param>
- JSON Array form
CMD ["<command>", "param"]
- Shell form
CMD
vsENTRYPOINT
CMD
allows to override Dockerfile command and argumentsdocker run my_container <command> <arguments>
ENTRYPOINT
only appends arguments into Dockerfiledocker run my_container <arguments>
- Set default arguments
- It must be JSON Array form
- You provide both
ENTRYPOINT
andCMD
1 2
ENTRYPOINT ["sleep"] CMD ["5"]
- Override
ENTRYPOINT
docker run --entrypoint <command> <image> <args>
docker run --entrypoint sleep2 ubuntu-sleeper 10
Networking
Network: Bridge
- Default network
- Network within Docker Host, allows communication between all containers
- You need to map Ports to access containers
- By default one Bridge Network is created
- If you need more, you have to specify this explicitly
Network: Host
- Ports are shared with the “real” network
- You don’t have Network isolation in this case (so beware of port collision with “real” network)
- Also you can’t run multiple containers on the same container ports, as they are now published
--network=host
Network: None
- No network - totally isolated
--network=none
Commands
- Create new Network
docker network create --driver <driver> --subnet <a.b.c.d/n> <network_name>
docker network create --driver bridge --subnet 182.18.0.0/16 my_network
- Embedded DNS
- All Docker containers are accessible by their names
- This DNS runs at
127.0.0.11
- Show Network info of container
- Use
docker inspect
- Use