Skip to content

Instantly share code, notes, and snippets.

@skplunkerin
Last active April 29, 2022 20:12
Show Gist options
  • Save skplunkerin/75eb153d2fa5151ff9d8be679479364f to your computer and use it in GitHub Desktop.
Save skplunkerin/75eb153d2fa5151ff9d8be679479364f to your computer and use it in GitHub Desktop.
docker cheatsheets

[WIP] Docker Cheatsheets


The docker-compose.yml file

  • [TODO - describe different parts of a docker-compose.yml file.]

The Dockerfile file


CLI Commands

For direct documentation, see the Docker CLI and Compose CLI references, found here.

Working with Images

  • # for the "Dockerfile" in current directory:
    docker build .
    # if you want to tag your build (making it easier to find/use later)
    docker build --tag project_name:tag .
    # if you have a different Dockerfile you want to use:
    docker build --tag project_name:multistage -f Dockerfile.multistage .

    This builds an image based off of the instructions in the Dockerfile file; when using the Dockerfile.multistage file it compiles the image to a smaller version (see the Multistage Dockerfile section for more info).

  • docker image ls

    This lists all images, example:

    REPOSITORY              TAG          IMAGE ID       CREATED             SIZE
    project_name            multistage   9983b3c98823   About an hour ago   18.8MB

Working with Containers:

  • # Run a built image that has the tag "project_name:multistage"
    docker run -p 3010:3010 --name project_name project_name:multistage
    # Run a built image that has the container id "36beab9d9323"
    docker run -p 3010:3010 --name project_name 36beab9d9323

    This runs the image in the terminal inside of a container with the name project_name (from --name).

  • # Run a built image that has the tag "project_name:multistage"
    docker run -d -p 3010:3010 --name project_name project_name:multistage
    # Run a built image that has the container id "36beab9d9323"
    docker run -d -p 3010:3010 --name project_name 36beab9d9323

    This runs the image in a detached container with the name project_name (from --name).

  • docker ps

    This lists all running containers:

    CONTAINER ID   IMAGE                        COMMAND            CREATED         STATUS         PORTS                    NAMES
    36beab9d9323   project_name:multistage      "/project_name…"   9 minutes ago   Up 9 minutes   0.0.0.0:3010->3010/tcp   project_name
    
  • docker ps --all

    This lists all containers (running, stopped, etc):

    CONTAINER ID   IMAGE                         COMMAND            CREATED         STATUS                    PORTS                    NAMES
    36beab9d9323   project_name:multistage       "/project_name…"   9 minutes ago   Up 9 minutes              0.0.0.0:3010->3010/tcp   project_name
    21abeb8a3932   project_name:multistage-test  "/project_name…"   10 minutes ago  Exited (2) 3 seconds ago                           project_name
    
  • docker stop project_name

    This stops a running container with the name of project_name.

  • docker restart project_name

    This restarts a stopped container with the name of project_name.

  • docker rm project_name

    This deletes a container with the name of project_name.


Working with Docker Compose:

  • # for the "docker-compose.yml" in current directory:
    docker-compose config
    # if you have a different compose file you want to use:
    docker-compose -f [docker-compose-dev.yml] config

    Validate the docker-compose.yml file before running it.

  • # for the "docker-compose.yml" in current directory:
    docker-compose up
    # if you have a different compose file you want to use:
    docker-compose -f [docker-compose-dev.yml] up

    Run the *last-built docker-compose.yml project in the terminal; use -f when you need a different docker-compose file (docker-compose.yml in the current directory is the default.)

    *"last-built" meaning that docker-compose up doesn't "re-build" anything, even if the code has changed; if you don't want the "last built", make sure you add --build to the command to trigger a "re-build"

    • # for the "docker-compose.yml" in current directory:
      docker-compose up -d
      # if you have a different compose file you want to use:
      docker-compose -f [docker-compose-dev.yml] up -d

      Run the *last-built docker-compose.yml project in detached mode.

    • # for the "docker-compose.yml" in current directory:
      docker-compose up --build -d
      # if you have a different compose file you want to use:
      docker-compose -f [docker-compose-dev.yml] up --build -d

      Build, then run the docker-compose.yml file in detached mode.

    • # for the "docker-compose.yml" in current directory:
      docker-compose up --build
      # if you have a different compose file you want to use:
      docker-compose -f [docker-compose-dev.yml] up --build

      Build, then run the docker-compose.yml file in the terminal.

      NOTE:

      Docker Compose is a useful tool, but it has its own quirks. For example, no rebuild is triggered on the update to the source code unless the --build flag is provided. It is a very common pitfall to edit one’s source code, and forget to use the --build flag when running docker-compose up. source

  • # for the "docker-compose.yml" in current directory:
    docker-compose down
    # if you have a different compose file you want to use:
    docker-compose -f [docker-compose-dev.yml] down

    This will remove the containers created during docker-compose up.

  • docker-compose ps

    List running docker-compose projects.

              Name                  Command           State                      Ports
    -----------------------------------------------------------------------------------------------------
    project_name                 /project_name        Up      0.0.0.0:80->3010/tcp,0.0.0.0:3010->3010/tcp

Working with Docker Context

Not sure what this is for, tbh?

(source: https://docs.docker.com/cloud/ecs-integration/)

Creating a Context

docker context create ecs myecscontext

Listing Contexts

docker context ls

Switching Context

docker context use myecscontext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment