Rashad Mirza logo
Docker Cheat Sheet for Beginners

Docker Cheat Sheet for Beginners

Rashad Mirza Nov 3, 2022
I'm not a DevOps guy, and I used Docker before on 2-3 projects as a beginner.

I took Bret Fisher's Udemy course and got my certificate recently.
During this course, I took some notes (especially commands) for myself.

I will share it with you, maybe it can be helpful for some of you.

I want to mention that, it's not a professional document, it's just my notes that I took during the course.

$ docker version
$ docker info

$ docker container run --publish 8888:80 --detach --name nginx nginx

- This command starts a new container
$ docker container run

- This command starts an existing stopped one
$ docker container start

- Stop container
$ docker stop {CONTAINER_NAME}
$ docker container stop {CONTAINER_ID}

- List of the containers
$ docker container ls (It will show Only running containers)
$ docker container ls -a (It wiill show All containers)

- Run in the background
$ --detach
$ --d

- Name
$ --name {NAME}

- Add env variables
$ -e MYSQL_RANDOM_ROOT_PASSWORD=yes

- To find created password run this:
$ docker container logs mysql

- Logs
$ docker container logs {CONTAINER_NAME}

- Monitor running processes in Linux
$ ps aux
$ ps aux | grep {SEARCH_TERM}

- Processes
$ docker ps
$ docker container top {CONTAINER_NAME}

- Delete container
$ docker container rm {CONTAINER_NAME}
$ docker container rm -f {CONTAINER_NAME}

- CURL
$ curl localhost:4000


- Inspect
$ docker container inspect {CONTAINER_NAME}

- Stats
$ docker container stats

- Get into container (Like SSH)
$ docker container run -it --name proxy nginx bash

- Stop bash container
$ exit

- Restart stopped bash container
$ docker container start -ai nginx

- See shell of existing running container
$ docker container exec -it mysql bash

- Pull an image
$ docker pull {IMAGE_NAME}

- Image list
$ docker image ls

- Port check
$ docker container port {CONTAINER_NAME}

Docker Networks


- IP Address of container
$ docker container inspect --format ‘{{ .NetworkSettings.IPAddress}}’ {CONTAINER_NAME}

- IP Address of my home network
$ ifconfig en0

- Network list
$ docker network ls

- Inspect Network
$ docker network inspect {NETWORK_NAME}

- Create a new network
$ docker network create {NETWORK_NAME}

- Create a new container with a given Network
$ docker container run -d --name {CONTAINER_NAME} --network {NETWORK_NAME} {IMAGE_NAME}

- Connect existing container to a new network
$ docker network container {CONTAINER_NAME} {NETWORK_NAME}

- Inspect container to see which network connected
$ docker container inspect {CONTAINER_NAME}

- Disconnect container from network
$ docker network disconnect {CONTAINER_NAME}
—--------------------

- Iterm 2 NEW TAB
CMD+SHIFT+D

--rm removes container after working on it.

- Image history
$ docker history {IMAGE_NAME}

- Create your own image
$ docker image tag {SOURCE_IMAGE:TAG} {TARGET_IMAGE:TAG}

- Docker login
$ docker login

- Docker Logout
$ docker logout

- Docker config file
$ cat .docker/config.json

- Building images
$ vim Dockerfile
$ docker image build -t {IMAGE_NAME} .

- Docker space usage
$ docker system df

- Docker clean up everything
$ docker system prune

- Clean up dangling images
$ docker image prune

- Remove all images you’re not using
$ docker image prune -a

- Swarm
$ docker swarm init

- Node list
$ docker node ls

- Create service
$ docker service create alpine ping 8.8.8.8

- Service list
$ docker service ls

- Docker service containers
$ docker service ps {SERVICE_NAME}

- Change number of replicas
$ docker service update {SERVICE_NAME} --replicas 3