Building Images in Docker

Building Images in Docker

1.To build images you should have some containers

syntax

docker commit -a|--author[=" "] -m|--message[=" "] CONTAINER [REPOSITORY[:TAG]]

example

docker commit -a "vishalturbo2" -m "ubuntu demo package" 0a15686588ef vishalturbo2/ubuntu:16.04

2.push your new image to docker hub

syntax

docker push NAME[:TAG]

example

docker push vishalturbo2/ubuntu:16.04

3.Building our own docker image with build instructions

1.simple docker file

create a directory

mkdir abcd
cd abcd

create a file name called dockerfile

nano dockerfile

for a basic image just use one command

syntax

FROM (os name:TAG)

example

FROM ubuntu

save the file and run below command

docker build .

(NOTE: space dot(.) is mandatory)

2.building docker image with instructions
  • FROM instruction

FROM command is an instruction to set a base image for a new docker image like ubuntu,fedora

syntax

FROM <images>:<tag>

example

FROM ubuntu:16.04

for 3rd party or private images

syntax

[registry_hostname[:port]/][user_name/](repository_
name:version_tag)

example

FROM registry-host:300/vishalturbo2/ubuntu:16.04
  • MAINTAINER instruction

this sets author name of image

syntax

MAINTAINER <name>

example

MAINTAINER vishalturbo2
  • RUN instruction

run command is used to execute the terminal commands in docker

example to install apache

RUN yum -y install apache2

example to move a file

RUN mv /wordpress/* /var/www/html/

example to give permissions

RUN chown -R apache:apache /var/www/
  • COPY instruction

The COPY instruction enables you to copy the files from the Docker host to the filesystem of the new image

syntax

COPY <source> ... <destination>

example

COPY abc /var/www/html/
  • ADD instruction

add function can handle URLs and TAR files

syntax

ADD <src> ... <dst>

example

ADD http://wordpress.org/latest.tar.gz /wordpress.tar.gz
  • EXPOSE instruction

expose instruction sets a port to the server ip

syntax

EXPOSE <port>

example

EXPOSE 80
  • CMD instruction

This is similar to run fuction but run acts while processing thye image but cmd acts after creating and setting up the container

syntax

CMD <command>

example

CMD ["/bin/bash", "/start.sh"]
Last updated on by vishal devxo