Useful Docker Commands
Tags: docker, container
Published: 2023-07-16 16:03:32

Docker is a platform for developing, shipping and running applications. In this post, we'll discuss about some of the salient Docker commands used in our projects frequently.

**docker pull**
This command helps to pull the image from docker hub. If we do not specify the tag to be pulled, latest docker image will be pulled.

docker pull image:tag

```
docker pull mysql:8.0.33
8.0.33: Pulling from library/mysql
e2c03c89dcad: Pull complete 
68eb43837bf8: Pull complete 
796892ddf5ac: Pull complete 
6bca45eb31e1: Pull complete 
ebb53bc0dcca: Pull complete 
2e2c6bdc7a40: Pull complete 
6f27b5c76970: Pull complete 
438533a24810: Pull complete 
e5bdf19985e0: Pull complete 
667fa148337b: Pull complete 
5baa702110e4: Pull complete 
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Status: Downloaded newer image for mysql:8.0.33
docker.io/library/mysql:8.0.33
```

**docker images**
Shows the docker images downloaded to the local machine.

```
docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        8.0.33    041315a16183   11 days ago   565MB
```

**docker run**
Runs the container from the docker image.

```
docker run -p6000:3306 --name some-mysql-new -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0.33
0dacb1101ad336475f7ebcbc5e21cc9fb894f2bfa4c0a805f65d3ef1f39bf7e4
```
-p(host port):(container port) - Host port gets bounded to the container port so that it can be accessible.
-d : Runs the container in the detached mode.

**docker ps**
Shows the list of running containers.

```
docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                               NAMES
0dacb1101ad3   mysql:8.0.33   "docker-entrypoint.s…"   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:6000->3306/tcp   some-mysql-new
```

**docker stop**
Stops a running docker container. The container id needs to be passed.

```
docker stop 0dacb1101ad3
```

**docker start**
Starts a docker container. The container id needs to be passed.

```
docker start 0dacb1101ad3
```

**docker logs**

Shows the logs of a Docker container. The container id needs to be passed.

```
docker logs 0dacb1101ad3
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Initializing database files
2023-07-16T14:02:16.555166Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:16.555434Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.33) initializing of server in progress as process 81
2023-07-16T14:02:16.562164Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:17.077951Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:18.844888Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-07-16 14:02:23+00:00 [Note] [Entrypoint]: Database files initialized
2023-07-16 14:02:23+00:00 [Note] [Entrypoint]: Starting temporary server
2023-07-16T14:02:23.967805Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:23.969395Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 123
2023-07-16T14:02:23.984575Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:24.258224Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:24.669973Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:02:24.670204Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:02:24.673431Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:02:24.732237Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:02:24.733339Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2023-07-16 14:02:24+00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2023-07-16 14:02:29+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-07-16T14:02:29.418853Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.33).
2023-07-16T14:02:31.320144Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
2023-07-16 14:02:31+00:00 [Note] [Entrypoint]: Temporary server stopped

2023-07-16 14:02:31+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2023-07-16T14:02:31.685058Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:31.686585Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
2023-07-16T14:02:31.694428Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:31.882615Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:32.331478Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:02:32.331559Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:02:32.334663Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:02:32.367684Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:02:32.367707Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2023-07-16T14:07:21.221136Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.33).
2023-07-16T14:07:22.110930Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-07-16T14:07:33.176975Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:07:33.178454Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
2023-07-16T14:07:33.185757Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:07:33.376968Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:07:33.689001Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:07:33.689518Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:07:33.693007Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:07:33.743641Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:07:33.744479Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
```

**docker exec**
Get into the terminal of the container.

```
docker exec -it 0dacb1101ad3 /bin/bash
bash-4.4# ls
bin  boot  dev	docker-entrypoint-initdb.d  entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
bash-4.4# exit
exit
```
-it refers to interactive.

**docker image rm**
Removes an image. Image name along with tag should be mentioned in the command.

```
docker image rm mysql:8.0.33 -f    
Untagged: mysql:8.0.33
Untagged: mysql@sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Deleted: sha256:041315a161837f8bb87361e13390abda7159b98aeedee5e6152a0bb7a9b45f27
```