Skip to content

Instantly share code, notes, and snippets.

@facelordgists
Last active March 19, 2020 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facelordgists/023b2e4093e0c4e6d45ee5aeb77ff278 to your computer and use it in GitHub Desktop.
Save facelordgists/023b2e4093e0c4e6d45ee5aeb77ff278 to your computer and use it in GitHub Desktop.

1. General Stuff

Terminology

An image is a set of layers. When you start an image, it becomes a running container. You can have many running containers of the same image.

An instance of an image is called a container.

The image is the recipe, the container is the cake. You can make as many cakes as you like with a given recipe. A stopped container is a cake in the freezer.

A volume is a virtual drive which enables us to persist data and share between containers.

Start / build environment

docker-compose up

Start as background process

docker-compose up -d

The -d flag backgrounds the process and log output. To view logs for a specific container, use docker-compose logs [container]

Stop vs Down

The docker-compose stop command will stop your containers, but it won’t remove them.

The docker-compose down command will stop your containers, but it also removes the stopped containers as well as any networks that were created.

You can take down 1 step further and add the -v flag to remove all volumes too. This is great for doing a full blown reset on your environment by running docker-compose down -v.

To show and follow the log output of the container execute:

docker logs -ft container_name

Copy files between host and container

https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container

mycontainer is a container ID, not an image ID. Get the image ID with docker ps

The cp command can be used to copy files. One specific file can be copied into the container like:

docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt

Multiple files contained by the folder src can be copied into the target folder using:

docker cp src/. mycontainer:/target
docker cp mycontainer:/src/. target

You can also copy files out of a container:

docker cp divihub-test_phpfpm_1:/usr/local/etc/php-fpm.d/www.conf .
docker cp <containerId>:/file/path/within/container /host/path/target

2. Containers

List Running Docker Containers

docker ps

List All Docker Containers

docker ps -a

SSH into a docker container

See: https://docs.docker.com/compose/reference/exec/

In this example wp is the service defined in the docker-compose.yml docker-compose exec <service> bash

docker-compose exec <docker_name> bash
docker-compose exec wp bash
# or
docker exec -ti <docker_name> /bin/bash
docker exec -ti divi-card-factory-rsmm_wp_1 /bin/bash

Kill all running docker containers:

docker kill $(docker ps -q)

Delete containers of current docker-compose

-s flag also shuts down running containers if needed

docker-compose rm -s

Delete a specific Docker container

docker rm <container_ID>

3. Images

Delete the dangling docker images

The ones that are not tagged properly and are hanging around usually as the result of the intermediate container creation:

docker rmi $(docker images -q -f dangling=true)

List docker images

docker images -a

remove a specific docker image

docker rmi Image Image

Recreating the images

Recreating the images is as simple as restarting the application. Leaves the volumes

docker-compose up --build

4. Volumes

Determine which volumes are used by a container

docker inspect --format="{{.Mounts}}" <container_id>

Delete specific volumes

docker volume ls
docker volume rm <name_of_volume>

Delete all volumes

docker volume prune

5. Cleanup & Reset

Purging All Unused or Dangling Images, Containers, Volumes, and Networks

docker system prune

To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:

docker system prune -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment