Skip to content

Instantly share code, notes, and snippets.

@arsane
Last active July 30, 2017 04:44
Show Gist options
  • Save arsane/1e819cdc6ebb802bb6c110b8ab8a6b24 to your computer and use it in GitHub Desktop.
Save arsane/1e819cdc6ebb802bb6c110b8ab8a6b24 to your computer and use it in GitHub Desktop.
useful docker command lines

Remove old container not used over 1 month.

docker ps --filter "status=exited" | grep 'months ago' | awk '{print $1}' \
| xargs --no-run-if-empty docker rm

Copy file between docker and host

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

Copy file inside docker from remote machine

function vcp() {
  ssh $1 "docker cp \`docker ps -a|grep $2|awk '{print \$1}'|head -n1\`:$3 -" | tar xvf -
}

Get container's ip address

docker inspect $CID | grep IPAddress | cut -d \" -f 4

ADD vs `COPY'

  • ADD allows <src> to be an URL.
  • If the <src> parameter of ADD is an archive in a recognised compression format, it will be unpacked

CMD vs ENTRYPOINT

When you run docker like this: docker run -i -t ubuntu bash the entrypoint is the default /bin/sh -c, the image is ubuntu and the command is bash.

The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash. This allowed docker to implement RUN quickly by relying on the shell's parser. Later on, people asked to be able to customize this so ENTRYPOINT and -entrypoint has been introduced.

Everything after ubuntu in the example above is the command and is passed to the entrypoint. When using the CMD instruction, it is exactly as if you were doing docker run -i -t ubuntu <cmd>. <cmd> will be the parameter of the entrypoint.

You will also get the same result if you instead type this command docker run -i -t ubuntu. You will still start a bash shell in the container because of the ubuntu Dockerfile specified a default CMD: CMD ["bash"].

EXPOSE

If you EXPOSE a port, the service in the container is not accessible from outside Docker, but from inside other Docker containers. So this is good for inter-container communication.

If you EXPOSE and -p a port, the service in the container is accessible from anywhere, even outside Docker.

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