Images handling

Listing Images

docker images

Removing Images

docker rmi hello-world:latest

Images can’t be deleted if they’re being used by a running container. Remove the container first, then the image.

Running

docker run demo-image:latest

Run detached containers

docker run --name nginx -d nginx:latest

Run with port forwarding into container

docker run --name nginx -p 8800:80 -d nginx:latest

8800 on your host to port 80 inside the container

Run with environment variables

docker run -e POSTGRES_PASSWORD=secret postgres

Run with limits

docker run -e POSTGRES_PASSWORD=secret --memory="512m" --cpus="0.5" postgres

Creating new image – Dockerfile

nano Dockerfile

Inside the dockerfile

# This specifies the base image that the build will extend
FROM ubuntu:18.04
# This instruction specifies the “working directory” or the path in the image where files will be copied and commands will be executed.
WORKDIR /usr/src/app
# This copy files to the container
COPY app.js .

# This instruction tells the builder to run the specified command.
RUN apt-get -yqq update
RUN apt-get -yqq install python3-pip python3-dev curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install -yq nodejs

# This define the port number the container should expose
EXPOSE 5000

# ENV – this instruction sets an environment variable that a running container will use.

# USER – this instruction sets the default user for all subsequent instructions

# This instruction sets the default command a container using this image will run
CMD [“app.js”]

Creating new image – Build

# To tag an image during a build, add the -t or –tag flag:

docker build -t demo-image:latest .

Change tag

docker image tag