Skip to content

Instantly share code, notes, and snippets.

@bekirbakar
Last active April 24, 2023 18:45
Show Gist options
  • Save bekirbakar/0f9bf6d455ebade25241ec0fa9ae520c to your computer and use it in GitHub Desktop.
Save bekirbakar/0f9bf6d455ebade25241ec0fa9ae520c to your computer and use it in GitHub Desktop.
Docker Quick Reference

Copy Files from Docker Container

To copy a folder from a running Docker container to your local machine, use the docker cp command. Here's how to do it:

First, find the container ID or name of the running container by listing all running containers:

docker ps

This command will show you the container ID, image name, created time, status, ports, and container name.

Use the docker cp command to copy the folder (e.g., "models") from the container to your local machine:

docker cp CONTAINER_ID:/path/to/models /local/path/for/models

Replace CONTAINER_ID with the actual container ID or name from the docker ps output. Replace /path/to/models with the path to the "models" folder inside the container, and /local/path/for/models with the destination path on your local machine.

For example, if the "models" folder is located at /app/models inside the container and you want to copy it to your local machine's /home/user/models directory, the command would be:

docker cp CONTAINER_ID:/app/models /home/user/models

This command will copy the "models" folder and its contents from the container to your local machine.

Docker Quick Reference

This gist provides a quick reference for common Docker commands, including commands for Dockerfiles, images, containers, networks, and Docker Compose.

Contents

Dockerfile Commands

Command Description
FROM [base image] Set the base image for the Dockerfile.
MAINTAINER [maintainer name] Set the maintainer of the Dockerfile.
RUN [command] Run a command during the build process.
ADD [source] [destination] Copy files from the build context to the container.
COPY [source] [destination] Copy files from the build context to the container.
CMD [command] Set the default command to run when the container starts.
ENTRYPOINT [command] Set the entry point for the container.

Image Commands

Command Description
docker image ls List all available images.
docker image rm [image ID] Remove an image.
docker image build -t [image name] [path to Dockerfile] Build a Docker image from a Dockerfile.
docker image push [image name] Push an image to a remote registry.
docker image pull [image name] Pull an image from a remote registry.

Save and Load

docker load < archive.tar.gz
docker save repository/name:latest > archive.tar

Docker Compose Commands

Command Description
docker-compose up Start all containers defined in the Docker Compose file.
docker-compose down Stop and remove all containers defined in the Docker Compose file.
docker-compose ps List all containers defined in the Docker Compose file.
docker-compose logs [service] Display logs for a service in the Docker Compose file.
docker-compose build Build all images defined in the Docker Compose file.
docker-compose config Validate and view the Docker Compose file.
docker-compose run [service] [command] Run a command in a new container for a service.

Containers Commands

Command Description
docker container ls List all running containers.
docker container ls -a List all containers, including stopped ones.
docker container rm [Container ID] Remove a container.
docker container stop [Container ID] Stop a container.
docker container start [Container ID] Start a stopped container.
docker container restart [Container ID] Restart a running container.
docker container pause [Container ID] Pause a running container.
docker container unpause [Container ID] Unpause a paused container.
docker container logs [Container ID] Display logs for a container.
docker container exec [Container ID] [command] Execute a command inside a running container.
docker container inspect [Container ID] Display detailed information about a container.

Networks Commands

Command Description
docker network ls List all available networks.
docker network create [network name] Create a new Docker network.
docker network rm [network name] Remove a Docker network.
docker network connect [network name] [Container ID] Connect a container to a network.
docker network disconnect [network name] [Container ID] Disconnect a container from a network.

Share

docker pull <image-name>
docker tag <source-image-name> <target-image-name>

Examples

docker build -t <image-name>
docker image ls
docker image rm
docker container run --name cntnr -p 80:80 cntnr:cntnr
docker container stop cntnr
docker container kill cntnr
docker network ls
docker container ls
docker container rm -f $(docker ps -aq)
logs --tail 100 cntnr
docker run -i -t <container-name-or-id> bash
docker exec -it <container-name-or-id> bash
docker ps

docker rm -f <container id>

docker rm -f $(docker ps -qa)

clear; docker rm -f $(docker ps -qa); .build.sh -w

docker logs apps_core?panel_1 --tail 100 -f

Interactive Running

To enter a running Docker container and explore its filesystem, use the docker exec command with the -it flag, which stands for interactive and TTY mode. Here's how to do it:

First, list all running containers using the docker ps command:

docker ps

Next, use the docker exec command with the -it flag followed by the container ID or name, and then specify the shell you want to use, usually /bin/bash or /bin/sh. For example:

docker exec -it CONTAINER_ID /bin/bash

Replace CONTAINER_ID with the actual container ID or name from the docker ps output.

Once you're inside the container, you can explore the filesystem using standard Linux commands like ls, cd, cat, and others. To exit the container and return to your host system, type exit and press Enter.

If you want to explore the filesystem of a Docker image that isn't running, you can create a temporary container from the image and then remove it when you're done:

docker run -it --rm IMAGE_NAME /bin/bash

Replace IMAGE_NAME with the actual name or ID of the Docker image you want to explore. This command will create a new container from the specified image, open an interactive shell, and automatically remove the container when you exit the shell.

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