Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Last active July 10, 2023 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwastaken/dc6d90136030b676f0cdf18da98ccc53 to your computer and use it in GitHub Desktop.
Save ericwastaken/dc6d90136030b676f0cdf18da98ccc53 to your computer and use it in GitHub Desktop.
A Docker / Docker Compose Cheat Sheet

Docker / Docker Compose Cheat Sheet

Docker

Add this to Dockerfile to add aliases of your choice (works for images that have /bin/bash)

# Add alias to user profile
RUN echo "alias ll='ls -la'" >> /root/.bashrc

Docker Compose

Running arbitrary commands on a Docker containter managed with Docker Compose

Execute the Docker Compose run command, from the same directory that contains your docker-compose.yml file.

# Simple form
docker compose run -it SERVICE-NAME-IN-COMPOSE-YML "COMMAND" "ARGS-TO-COMMAND"

# Simple form, to get a shell
docker compose run -it SERVICE-NAME-IN-COMPOSE-YML "/bin/bash"

# If you need to override the entrypoint
docker compose run -it --entrypoint /bin/bash SERVICE-NAME-IN-COMPOSE-YML "COMMAND" "ARGS-TO-COMMAND"

run will ALWAYS start a new instance of a container!

In contrast, exec will operate on a running container (and won't create a new instance)!

Note: On some platforms, use docker-compose instead of docker compose.

Force Emulation on Docker Images that don't have an ARM build (on an Apple Silicon Mac):

The Docker for Mac app comes packed with QEMU, a popular OSS emulator. Therefore, Docker can run both arm64 and amd64 images on Macs with Apple Silicon.

Read more here: (scroll to “Using Docker”) https://earthly.dev/blog/using-apple-silicon-m1-as-a-cloud-engineer-two-months-in/

If you have an Apple Silicon Mac and you need to run a Docker image that does not have an ARM build, then you can force Docker to download an amd64 image and emulate.

Some ways to do this: (there’s probably others, like docker pull etc.)

  • If you’re using Docker Compose with no Dockerfile to build from, for the particular service, just include platform: linux/amd64.
  • If you’re using a Dockerfile, use the --platform switch in the FROM. For example FROM --platform=linux/amd64 centos .

References:

License

This content is shared under a Creative Commons Attribution-ShareAlike 4.0 International.

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