Docker Commands Reference

Docker Run

Build and start a Docker container from an image.


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


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

Docker Start

Start an already built but stopped container

Docker Inspect

Get detailed information about a specified container

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

Docker Exec

Run a command on a specified container

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

docker exec -it testcontainer /bin/bash

Docker Container

Manage docker containers, such as list them

List Containers

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

docker container ls -a

Remove all Stopped Containers

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

sudo chgrp `whoami` /run/docker.sock

SSH into an image

I always forget this. First run this command

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

Which should give output like this

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

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