Skip to main content

What is an Image

  • Images are app binaries and dependencies with metadata about the image data and how to run the image
  • we can have image in two ways
    1. Pull an already made from docker-hub or
    2. Build our own

Pull Existing Image

docker pull [image_name]

Example

docker pull NGINX
docker pull MONGODB

Once we have our image we can run it 4. containers

Build Your Own Image

Best to use docker-hub-username/project-name as your image name

  • After creating a dockerfile run the following command [[Containerization/Docker/2. dockerfiles]]
docker build -t tag_name path_to_docker_file
# -t = tag followd by tag/name
docker build -t zekaryas/hello_world .

The Dockerfile is Not Named Dockerfile

  • In the above command docker assumes there is a file called dockerfile in the path specified
docker build -t awsome_project -f not_named_dockerfile .

List Images We Have

docker image ls

Remove an Image

docker image rm [image_name]

Remove All Image

docker rmi $(docker images -a -q)

Push Image to Docker-hub

You might need to run ==docker login== for this to work

docker image push [image_name]
docker image push zekaryas/hello-world

Tagging a Container

  • Tag is an additional name/info we attach to an image to make it more meaningful
  • The Docker tag command creates a new tag for an image.
  • It does not create a new image. The tag points to the same image and is just another way to reference the image.

Create an Image with Tag

docker build -t zekaryas/hello_world .

Give a Tag to Existing Image

docker pull nginx  #we have image with tag: latest

docker image tag nginx zekaryas/nginx #we've the same image with a new tag name zekaryas/nginx

docker image tag nginx nginx:v1:12:12 #we've the same image with a new tag name nginx:v1:12:12

docker image tag prev_tag_name new_tag_name #syntax

Joining Multiple Docker Images into One

  • If you have a project that requires multiple docker images you've these options
    • Use docker-compose, but if docker-compose isn't allowed…
      • Start From ubuntu and install & setup the required stacks like you would in a normal computer
        • or start from one base image(ex: node, python…)
      • Or use docker-multi stage build where you can combine multiple images to run a project

for example: The project could be a node.js project that also has mysql as a db…

# Use a base image with Node.js and MySQL pre-installed
FROM node:14

# Set up the working directory for the Node.js app
WORKDIR /app

# Copy package.json and package-lock.json to container
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy the rest of the application source code to container
COPY . .

# Expose the port on which your Node.js app is listening
EXPOSE 3000

# Set environment variables for MySQL
ENV MYSQL_HOST=localhost \
MYSQL_USER=root \
MYSQL_PASSWORD=secret \
MYSQL_DATABASE=mydatabase

# Install MySQL server inside the container
RUN apt-get update && apt-get install -y mysql-server

# Run the MySQL server as a background service
RUN service mysql start

# Import SQL script to initialize the database (if needed)
COPY init.sql /docker-entrypoint-initdb.d/init.sql

# Start the Node.js app
CMD ["npm", "start"]

Multi-state Build

  • Multi-state let's you add multiple FROM statements to your Dockerfile, while allowing you
    • to use different bases on each FROM
    • to move artifacts or builds from one base to another
    • faster image build on subsequent builds
  • Here is an example of a multi-state dockerfile with two stages
    • stage 1: Builds the react app using node.js base
    • stage 2: Creates production image with NGINX base
# Stage 1: Build React app
FROM node:latest as build

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm install

COPY . .
RUN npm run build

# Stage 2: Create production image with NGINX
FROM nginx:latest

# Remove default NGINX static content
RUN rm -rf /usr/share/nginx/html/*

# Copy built React app from previous stage to NGINX container
COPY --from=build /app/build /usr/share/nginx/html

# Copy custom NGINX configuration if needed
# COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf

# Expose port 80
EXPOSE 80

# Start NGINX server
CMD ["nginx", "-g", "daemon off;"]