How to redeploy a docker image using jenkin pipeline? - docker

I created a pipeline for spring boot microservice project. I am automating the deployment process using jenkin pipeline.
The steps which I used in pipeline as follows:
Jenkin script first checkout code from bitbucket.
Build a project using maven.
Create a docker image.
Push docker image to dockerhub.
Then run this docker image by downloading docker image from docker hub.
It works perfectly one time. It would works second time as I need to stop docker conatiner and then remove image from there.
.
I used docker run -rm According to documentation -rm is used to removed image form docker. But this not working any one help me out in this case
docker run --rm -p 8761:8761 -d --name ccpserviceregistry mydockerRepo/ccpserviceregistry:1.0
Want to redeploye the image with latest one .

Follow these steps:
Checkout code from bitbucket
Build project using maven
Create docker image
Push docker image to dockerhub
Remove if any docker container already running docker rm -f container-name
Remove docker image if you want to if any (docker rmi -f image-name)
Run docker image (use --name option in docker run so that it will be easier while removing the container, no need to provide --rm option)
Hope this helps.

Related

When rebuilding in Docker is needed?

Can we run a container, install software dependencies, and then use the updated container without building again the image?
You can re-use the container on the system you created it on until you delete the container, even if you exit the container. You can give the container a name at the time you first run it; e.g.:
docker run -it --name my-container ubuntu
After you exit, you can restart and reattach to the container with:
docker start my-container && docker attach my-container
Note that if you docker run with the --rm option, the container is deleted when you exit, and so you won't be able to restart it.
If you wish to turn the current state of your container (with all of its installed software dependencies, etc) into a new docker image that you can docker run or even docker push to Dockerhub, etc., you can do that with:
docker commit my-container my-image[:<tag>]
At that point,
docker run -it --rm my-image
will create and run a new container that starts from the state your original container was in when it was committed. It's exactly the same as if you had built the new image from a Dockerfile that had run all the commands you issued in the container before it was committed.
Having said all that, it's generally a better idea to build your desired docker image using a Dockerfile, because then the build steps are repeatable, and you can make changes to the build steps (e.g., what prerequisites are needed) without having to start from scratch.

Official Docker image says docker not running?

I perform the following docker commands in the following order:
docker pull docker
docker run -ti <imgId>
https://hub.docker.com/_/docker/
Now I am inside the "docker" image for Docker
Now suppose I create a temp folder and download a Dockerfile
mkdir temp
cd temp
curl <dockerfile>
docker build .
It will tell me Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
This means that the docker service needs to be started, but as the official docker image comes on alpine linux, commands like service/systemctl are not available, so we must perform apk add openrc --no-cache to access these.
After I install it, I still cannot start the docker service.
Performing system docker start says that it cannot find docker as a service?
service: service docker does not exist
Eventually I want to build this via Jenkins.
In the build step, I perform Execute Shell
if [ -f "Dockerfile" ]; then
echo "Dockerfile exists ... removing it"
rm Dockerfile
fi
wget <dockerFile url>
docker build .
I purposely don't do the openrc on Jenkins since I want to test locally first
The image you're pulling here (with the latest tag) does not contain the docker daemon. It's meant to be used as the docker client. What you want is to first get the docker daemon running with the image tagged dind (docker in docker).
docker network create dind
docker run --privileged --name docker --network dind -v docker-client-certs:/certs/client -d docker:dind
To verify it started up and works, you can check the logs.
docker logs docker
Now you can use a client container to connect to the daemon. This is how you connect interactively to the shell, like you wanted to:
docker run -ti --network dind -e DOCKER_TLS_CERTDIR=/certs -v docker-client-certs:/certs/client:ro docker
Docker commands should work inside this container. If you do docker version, you should see the versions of both the client and the server.
Note the two containers share the same network (some examples online feature links, but those are deprecated). They also share some of the TLS certs, which are generated when starting up the dind image.

Where does the docker images stored in local machine

I created docker a docker image of a spring boot application using the below command
docker build -f Dockerfile -t myimage
once i run the command "docker images" I see that image. Now I want to get that image out my local machine and run on another machine using the below command.
docker run -p 8085:8085 myimage
What are the steps to relocate docker image to another machine over a physical medium?
/var/lib/docker/images/overlay2/imagedb/content
but that local contain .txt files of 6KB. I know normally a docker image is around 600MB in size.
Could anyone please help me to find the exact location.
Thank you
1.You can export your docker image after building it.
docker build -f Dockerfile -t myimage .
docker save myimage > myimage.tar You will see this in your directory where you execute docker build command.
Then you can load it anywhere else as
docker load < myimage.tar
Other than that if there is a docker repo , you can simply push the docker image.
Reference:- https://docs.docker.com/engine/reference/commandline/export/#options
2.If the other machine can be reachable via a network you can setup the other machine as a docker hub

Using docker pull & run to build dockerfile

I'm learning how to use docker.
I want to deploy a microservice for swagger. I can do
docker pull schickling/swagger-ui
docker run -p 80:8080 -e API_URL=http://myapiurl/api.json swaggerapi/swagger-ui
To deploy it, I need a dockerfile i can run.
How do i generate the dockerfile in a way I can run it with docker build ?
The original question asks for a Dockerfile, perhaps for some CI/CD workflow, so this answer addresses that requirement:
Create a very simple Dockerfile beginning with
FROM schickling/swagger-ui
Then from that directory run
$ docker build -t mycontainername .
Which can then be run:
$ docker run -p 80:8080 -e API_URL=http://myapiurl/api.json mycontainername
Usually the docker pull pulls the Dockerfile. The Dockerfile for swagger is on the docker repo for it if you wanted to edit it or customize it.
(https://hub.docker.com/r/schickling/swagger-ui/~/dockerfile/)
That one should work with the build command. The build command builds the image, the run command turns the image into a container. The docker pull command should pull the image in. You don't need to run docker build for it as you should already have the image from the pull. You only need to do docker run.

Docker image versioning and lifecycle management

I am getting into Docker and am trying to better understand how it works out there in the "real world".
It occurs to me that, in practice:
You need a way to version Docker images
You need a way to tell the Docker engine (running on a VM) to stop/start/restart a particular container
You need a way to tell the Docker engine which version of a image to run
Does Docker ship with built-in commands for handling each of these? If not what tools/strategies are used for accomplishing them? Also, when I build a Docker image (via, say, docker build -t myapp .), what file type is produced and where is it located on the machine?
docker has all you need to build images and run containers. You can create your own image by writing a Dockerfile or by pulling it from the docker hub.
In the Dockerfile you specify another image as the basis for your image, run command install things. Images can have tags, for example the ubuntu image can have the latest or 12.04 tag, that can be specified with ubuntu:latest notation.
Once you have built the image with docker build -t image-name . you can create containers from that image with `docker run --name container-name image-name.
docker ps to see running containers
docker rm <container name/id> to remove containers
Suppose we have a docker file like bellow:
->Build from git without versioning:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments
in here:
fecomments is branch name and comments is the folder name.
->building from git with tag and version:
sudo docker build https://github.com/lordash/mswpw.git#fecomments:comments -t lordash/comments:v1.0
->Now if you want to build from a directory: first go to comments directory the run command sudo docker build .
->if you want to add tag you can use -t or -tag flag to do that:
sudo docker build -t lordash . or sudo docker build -t lordash/comments .
-> Now you can version your image with the help of tag:
sudo docker build -t lordash/comments:v1.0 .
->you can also apply multiple tag to an image:
sudo docker build -t lordash/comments:latest -t lordash/comments:v1.0 .

Resources