Docker provides us with the Docker store. Just like the Apple iStore or Google Play store, this is where we can pull ready to go images from.
In many cases somebody else already created an image that is very similar to the image we need. For example there already are images for MySQL or NGINX containers. So there is no need to create a new image.
We can explore the existing images in the store by visiting the following url:
To retrieve an image from the Docker store we can use the pull
command. For example pulling the official MySQL image (latest version):
docker pull mysql/mysql-server
To see the images that are available on our machine we can use the images
command:
docker images
After executing it we will see some output similar to this:
REPOSITORY TAG IMAGE ID CREATED SIZE
laradock_php-fpm latest 7e6e3e0c8c07 12 days ago 401MB
laradock_mysql latest 7a7ff6dd9ded 2 weeks ago 408MB
<none> <none> da40e81615c9 2 weeks ago 266MB
<none> <none> 04488e64901e 2 weeks ago 266MB
laradock_phpmyadmin latest bb80ff65b292 3 weeks ago 110MB
mysql 5.7 11615e225c92 3 weeks ago 408MB
<none> <none> adfffe2c670b 4 weeks ago 401MB
laradock_workspace latest 3f0110a3d4e2 4 weeks ago 764MB
<none> <none> 486837a0b550 4 weeks ago 266MB
laradock_nginx latest a5c6876188a7 4 weeks ago 21.1MB
mysql 8.0 c01d431b7858 3 months ago 266MB
phpmyadmin/phpmyadmin latest 200931982ab6 4 months ago 110MB
nginx alpine ba60b24dbad5 4 months ago 15.5MB
laradock/workspace 1.8-71 b88aa2c85533 7 months ago 691MB
laradock/php-fpm 1.4-71 a8cca8d57319 7 months ago 400MB
adminer 4.3.0 81e7e092910b 8 months ago 59.2MB
tianon/true latest 1298b2036003 8 months ago 125B
hello-world latest 48b5124b2768 10 months ago 1.84kB
When we want to start a container for the first time, we should use the run command. It allows us to optionally specify some configuration values that are specific to this container (but not to the image). For example starting a container for the first time based on the official MySQL image:
docker run --name=mysql1 -d mysql/mysql-server:latest
If the image has not been pulled before, the command will pull this image for us and start it immediately after pulling it.
The command above will give the name "mysql1" to the container. By specifying the -d
option we tell Docker that we want to start the container in the background. Otherwise our terminal would be attached to the console of the MySQL server and we can only see the output of that application.
A few options that are quite useful are:
-p
or --publish
(Allowing us to specify a port mapping from the container to the host)-v
or --volume
(Allowing us to mount a folder on the host file system inside the container)For example:
docker run --name=mysql1 -p 3306:3306 -v /path/to/folder/on/host:/var/lib/mysql mysql/mysql-server:latest
Will map port 3306
inside the container to port 3306
on the host and mount /path/to/folder/on/host
on the host file system inside the container in folder /var/lib/mysql
.
With this command we can see the containers that are available/running on our system. By default if we don't specify any other options we can see which containers are currently running:
docker ps
Will show some output similar to this (when there are containers running):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69b75c8faf22 laradock_nginx "nginx" 3 days ago Up 6 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp laradock_nginx_1
117b00ef6e93 laradock_php-fpm "docker-php-entryp..." 3 days ago Up 6 seconds 9000/tcp laradock_php-fpm_1
fa3f8904fb5f laradock_phpmyadmin "/run.sh phpmyadmin" 3 days ago Up 5 seconds 0.0.0.0:8080->80/tcp laradock_phpmyadmin_1
13507424a09e laradock_workspace "/sbin/my_init" 3 days ago Up 7 seconds 0.0.0.0:2222->22/tcp laradock_workspace_1
c471fa1fa201 laradock_mysql "docker-entrypoint..." 3 days ago Up 5 seconds 0.0.0.0:3306->3306/tcp laradock_mysql_1
When specifying the -a
option we will also be able to see the containers that are currently not running:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43e7406fc94e laradock_nginx "nginx" 32 seconds ago Up 31 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp laradock_nginx_1
4d8e6ca40e8c laradock_php-fpm "docker-php-entryp..." 33 seconds ago Up 32 seconds 9000/tcp laradock_php-fpm_1
b386eed3d950 laradock_workspace "/sbin/my_init" 34 seconds ago Up 33 seconds 0.0.0.0:2222->22/tcp laradock_workspace_1
869c41f96794 laradock_phpmyadmin "/run.sh phpmyadmin" 34 seconds ago Exited (0) 6 seconds ago laradock_phpmyadmin_1
e8db867e6f64 tianon/true "/true" 34 seconds ago Exited (0) 34 seconds ago laradock_applications_1
5f5aba08592b laradock_mysql "docker-entrypoint..." 34 seconds ago Up 34 seconds 0.0.0.0:3306->3306/tcp laradock_mysql_1
As we can see in the last output of the previous section, there are some containers available that are not running.
Of course Docker provides a command that we can use to start them:
docker start laradock_phpmyadmin_1
This will start the container using the configuration specified using the run
command without having to repeat it.
If the container was using any resources like a mounted folder from the file system, it will use the same resource again after being started.
Surely once we've started a container, there will come a time that we would like to stop a container. Docker allows us to stop a container with the same ease as starting one:
docker stop laradock_phpmyadmin_1
Note that this will only stop our container and not clean up any of the resources it is using.
At some point we don't need a certain container anymore and we will want to remove it. That can be achieved using the rm command:
docker rm laradock_phpmyadmin_1
The command above will delete the container. By default it will not remove the storage space (volume) that the container was using though. In most cases we will probably want to remove the volumes together with the container:
docker rm -v laradock_phpmyadmin_1
* Note that this will not work for named volumes, we will have to remove those manually.
Just like there will be a point in time where we are going to want to delete a container, there will be a time we will want to remove one or more images (after upgrading to the latest version for example). To achieve this we can either use the "image id" or the repository name and the tag:
docker rmi bb80ff65b292
or
docker rmi phpmyadmin/phpmyadmin:latest
At some point it will probably become necessary to execute some commands inside of the container. That can be done with the exec
command.
For example if we want to use bash inside the container we can use the following command:
docker exec -it laradock_workspace_1 bash
Doing the same, but specifying a user account:
docker exec -it --user=laradock laradock_workspace_1 bash
We can replace bash with a different command to execute that command.
Sometimes it can be necessary to retrieve the configuration details of a container. That can be done using the inspect
command:
docker inspect laradock_phpmyadmin_1
[
{
"Id": "b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84",
"Created": "2017-11-20T09:14:45.365032462Z",
"Path": "/run.sh",
"Args": [
"phpmyadmin"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 14344,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-11-20T09:14:46.157889826Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:8e563eeda3831860a60becf5e85dcd9d8720b7d1da4237b8346a2e705af8f3c4",
"ResolvConfPath": "/var/lib/docker/containers/b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84/hostname",
"HostsPath": "/var/lib/docker/containers/b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84/hosts",
"LogPath": "/var/lib/docker/containers/b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84/b2e6604c57b1d379c5a2fc17f7c77819f85b9473f76e53364e6c9e439addde84-json.log",
"Name": "/laradock_phpmyadmin_1",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "laradock_frontend",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"RestartPolicy": {
"Name": "",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": {
"DeviceId": "6848",
"DeviceName": "docker-253:0-2500328-5f596b49d6d05b714e3a94bd30682ee1b9e80172c577964b8ee2f3121ef41f84",
"DeviceSize": "10737418240"
},
"Name": "devicemapper"
},
"Mounts": [
{
"Type": "volume",
"Name": "26e995a133b7f2fec3e7cb7e7da9353eaac677cf53202a95888d06fd575dbdcc",
"Source": "/var/lib/docker/volumes/26e995a133b7f2fec3e7cb7e7da9353eaac677cf53202a95888d06fd575dbdcc/_data",
"Destination": "/sessions",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "b2e6604c57b1",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PMA_ARBITRARY=1",
"MYSQL_USER=default",
"MYSQL_PASSWORD=secret",
"MYSQL_ROOT_PASSWORD=secret",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"VERSION=4.7.5",
"URL=https://files.phpmyadmin.net/phpMyAdmin/4.7.5/phpMyAdmin-4.7.5-all-languages.tar.gz"
],
"Cmd": [
"phpmyadmin"
],
"ArgsEscaped": true,
"Image": "laradock_phpmyadmin",
"Volumes": {
"/sessions": {}
},
"WorkingDir": "",
"Entrypoint": [
"/run.sh"
],
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "dcb2fadb3db4a8fd4ca756e98a0f8cdd2df0a4b5955cd71ed6c5938ccd220a24",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "laradock",
"com.docker.compose.service": "phpmyadmin",
"com.docker.compose.version": "1.16.1",
"version": "4.7.5"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "6660686f2b77c4f26bc7a0647918840ddad8f6fe7fa1c914195b869aa975b2d5",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/6660686f2b77",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"laradock_backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"phpmyadmin",
"b2e6604c57b1"
],
"NetworkID": "b3821527b2ad3e40e5755fb0aa40a77fdb1ae6dcd27293ed91093d7c34940469",
"EndpointID": "9a317fe89e2d9fc60b6dc41d420165d4ebca7e1862f77fac9e875222cf86b5a7",
"Gateway": "172.22.0.1",
"IPAddress": "172.22.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:16:00:04",
"DriverOpts": null
},
"laradock_frontend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"phpmyadmin",
"b2e6604c57b1"
],
"NetworkID": "fb7288eeb5c04b210a804ad09b78b57943e1b32ec2c28e28560497b4f8e130d1",
"EndpointID": "99f63a70ad90f634667fd2eb27b1a5df075b818aadb59b00a7583ba7c23e7d74",
"Gateway": "172.21.0.1",
"IPAddress": "172.21.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:15:00:03",
"DriverOpts": null
}
}
}
}
]
As we can see the output can grow quite large, so it is recommended to use grep
to find the information we're looking for.
It can be useful to be able to look at the log a certain container is producing. Reasons may vary from finding out why a container is crashing to seeing if some event happened. This can be done using the logs command:
docker logs laradock_phpmyadmin_1
The output that will be displayed depends on the application(s) running inside the container and the way the image/container works.