Skip to content

Instantly share code, notes, and snippets.

@tysonpaul89
Created October 12, 2020 03:52
Show Gist options
  • Select an option

  • Save tysonpaul89/43793d34ed6531f03499f16a3a907ad8 to your computer and use it in GitHub Desktop.

Select an option

Save tysonpaul89/43793d34ed6531f03499f16a3a907ad8 to your computer and use it in GitHub Desktop.
Docker Cheat Sheet

Docker Cheat Sheet

To list all running containers

$ docker ps

To list all running and stopped containers

$ docker ps -a

To see advanced settings of a container

docker inspect ubuntu

To run a container(in attached mode)

$ docker run nginx

Press Ctrl + C to stop a container in attached mode.

To run a container in detached mode

$ docker run -d nginx

To stop a container in detached mod

$ docker stop <CONTAINER ID>

To remove a container

Stop the container if running, then remove the container.

$ docker stop <CONTAINER ID>
$ docker rm <CONTAINER ID>

To remove multiple containers

$ docker rm <CONTAINER ID> <CONTAINER ID> <CONTAINER ID>

To remove containers forcefully

$ docker rm -f <CONTAINER ID>

To list all docker images

$ docker images

To remove a docker image

$ docker rmi <IMAGE ID>

To run a container and expose a port

To run a container with port 38282 of the host system pointing to 8080 of the container.

docker run -p 38282:8080 kodekloud/simple-webapp:blue

To build an image from Dockerfile using a tag name

$ docker build -t <<tag-name>> /path/to/Dockerfile

To check Operating System used by the image

docker run <IMAGE NAME>:<TAG NAME> cat /etc/*release*
$ docker run python:3.6 cat /etc/*release*

To run a command in Dockerfile

CMD sleep 10

or

CMD ["sleep", "10"]

To provide input to a command in Dockerfile

...
ENTRYPOINT["sleep"]

Now we can run docker run ubuntu-sleep 10 and 10 will be appended in the dockerfile like sleep 10

To provide defualt value take if input is not given in Dockerfile

...
ENTRYPOINT["sleep"]

CMD["5"]

Now we if we run docker run ubuntu-sleep without any argument, then the argument in the CMD will appended to ENTRYPOINT and sleep 5 will be executed

Networking

Docker has three types of networks

  • Bridge
  • None
  • Host

In bridge network is a private network created by docker. Its usally start with the IP address 172.17.X.X series. Its the default network used by the docker. Example:

docker run ubuntu

None networker isolate the docker contiainer and wouldn't be accessable to external network. Example:

docker run ubuntu --network=none

In Host network, the port of the container is attached to the port of the host. Non network isolation is Example:

docker run ubuntu --network=host

To create user-defined bridge network for a container

docker network create \
--driver bridge \
--subnet 182.18.0.0/16 ubuntu

Storage

NOTE: The data stored in the container is not permanent. When the container goes down, the data in it will be erased. To presist data use volumes.

Volume mouting

# Old method
docker run -v data_volume:/var/lib/mysql mysql

# New method
docker run --mount type=volume,source=data_volume,target=/var/lib/mysql mysql

This command will create a new folder named data_volume in the /var/lib/docker/volumes/ directory in the host system and sync the data in container path /var/lib/mysql.

Bind mounting

# Old method
docker run -v /data/mysql:/var/lib/mysql mysql

# New method
docker run --mount type=bind,source=/data/mysql,target=/var/lib/mysql mysql

This command will bind the existing host directory /data/mysql to the container path /var/lib/mysql.

Docker Compose

Compose is a tool for defining and running multi-container Docker applications. Its uses a yaml file for configuring the services

Example: The following docker run commands can be made into a single yml file and running a command like docker-compose up will start the containers.

# In-memory storage
docker run -d --name=redis redis

# Database
docker run -d --name=postgres:9.4

# Backend
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app

# Frontend
docker run -d --name=result -p 5001:80 --link db:db result-app

# Worker
docker run -d --name=worker --link db:db --link redis:redis worker
# docker-compose.yml
version: 3
services:
	redis:
		image: redis
	db:
		image: postgres:9.4
	vote:
		image: voting-app
		ports:
			- 5000:80
		links:
			- redis:redis
	result:
		image: result-app
		ports:
			- 5000:80
		links:
			- db:db
	worker:
		image: worker
		links:
			- redis:redis
			- db:db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment