Docker

Docker Commands Reference

Docker Run

Build and start a Docker container from an image.

https://docs.docker.com/engine/reference/run/

Note

You can only use docker run once, unless you explicity remove the container once you are done.

Useful flags

Here are some useful flags you might include

-d

Run the container in the background (detached)

--name {container-name}

Specify a container name instead of it getting assigned a random one

-p 1234:1234

Specify port forwarding between local and container

--mount source=/abs/path/to/host/folder,target=/path/in/container,type=bind

Bind mount a directory from your host machine inside the container

https://docs.docker.com/storage/bind-mounts/

Docker Start

Start an already built but stopped container

https://docs.docker.com/engine/reference/commandline/start/

Docker Inspect

Get detailed information about a specified container

https://docs.docker.com/engine/reference/commandline/inspect/

1
2
docker run -d --name testcontainer {...}
docker inspect testcontainer

Docker Exec

Run a command on a specified container

https://docs.docker.com/engine/reference/commandline/exec/

For example, to get a bash shell on a running container:

1
docker exec -it testcontainer /bin/bash

Docker Container

Manage docker containers, such as list them

https://docs.docker.com/engine/reference/commandline/container/

List Containers

https://docs.docker.com/engine/reference/commandline/container_ls/

You need to specify the -a to get all containers!

1
docker container ls -a

Remove all Stopped Containers

https://docs.docker.com/engine/reference/commandline/container_prune/

1
docker container prune

Issues had with docker and how to fix them

Permission issues when trying to start an image

This is almost certainly because the user you are running as doesn't have access to the socket.

There is bound to be a better fix for this, in the meantime run

1
sudo chgrp `whoami` /run/docker.sock

SSH into an image

I always forget this. First run this command

1
$ docker ps  --format 'table {{.Names}}\t{{.Image}}'

Which should give output like this

1
2
3
NAMES                         IMAGE
test_test_1         wwwmagento2testdevelopmagentocouk_test:latest
test_test_mysql_1   wwwmagento2testdevelopmagentocouk_test_mysql:latest

Then run the following to get a prompt within the image

1
docker exec -it test_test_1 /bin/bash

After you run docker-compose up

You will get a screen with lots of text scrolling past until that stops and it looks like the process is hanging or has crashed.

This is normal behaviour, and you need to open a new terminal to interact with the image(s).

It took me significantly longer than I like to admit to figure that out