Skip to content

Instantly share code, notes, and snippets.

@haixuanc
Last active March 14, 2020 00:21
Show Gist options
  • Save haixuanc/1f425526dc85ef52deb6 to your computer and use it in GitHub Desktop.
Save haixuanc/1f425526dc85ef52deb6 to your computer and use it in GitHub Desktop.
Essential Docker commands

Basic Commands

View all commands:

$ docker

Search for images

$ docker search <string>

Download image:

$ docker pull <username>/<repository>

Usually we can specify a container with the first three or four digits of its ID.

Inspect a container:

$ docker inspect <containerid>

Access a running container:

$ docker exec -it <container-name/-id> bash

View logs of a container:

$ docker logs -f <container-id/-name>

View the host port that a container app's port maps:

$ docker port <container_name> <container_app_port>

Run a command from an image/container:

$ docker run <username>/<repository> <command args...>

Run the image on the same network stack of the host:

$ docker run --net=host <username>/<image>

Install things to a Ubuntu image (use -y flag to disable interactive download mode.)

$ docker run <username>/<ubuntu_image> apt-get install -y <software>

Push an image (you can only push an image under your namespace):

$ docker push <username/repository>

Print image logs (-f acts like tail -f):

$ sudo docker logs -f <container_id>

Examine processes running inside a container:

$ docker top <container_name>

Inspect a container:

$ docker inspect <container_name>
$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' <container_name>

(Re)start a container:

$ docker start <container_name>
$ docker restart <container_name>

Commit changes to a container

Find out container id:

$ docker ps --latest

Save this container with a new repository name:

$ docker commit <containerid> <username/new_repo>

Delete an image

Look up container id(s):

$ docker ps -a | grep <image_name>

Stop running the container:

$ docker stop <container_id>
```

Remove container:

``` bash
$ docker rm <container_id>
```

Remove image:

``` bash
$ docker rmi <image_id>
```

### Delete all docker containers

List all docker containers:

``` bash
$ docker ps -a
```

Kill and delete all docker containers:

``` bash
$ docker rm $(docker kill $(docker ps -q))
```

### Delete all docker images

List all installed docker image ids:

``` bash
$ docker images -q
```

Delete all docker images:

``` bash
$ docker rmi $(docker images -q)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment