How to specify current directory for Docker? - macos

I'm following this part of the Docker tutorial (on a Mac): https://docs.docker.com/mac/step_four/. I'm getting an error when I try to run the docker-whalesay image because it can't find fortunes.
I started off in the Dockerfile using /user/games/fortunes. Then I changed to just fortunes. Neither work.
How do I specify in the Dockerfile to use the current folder (mydockerbuild)?

The Dockerfile in that example does not rely on files that are present on your computer, basically, the only steps needed are;
Create an empty directory (you named it mydockerbuild)
mkdir mydockerbuild
Change to that directory
cd mydockerbuild
Create a Dockerfile
Edit the Dockerfile to look like this;
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
Build the Dockerfile, and name the built image "docker-whale"
docker build -t docker-whale .
Run the image you just built
docker run --rm docker-whale
The /usr/games/fortunes path in the Dockerfile is referring to a path inside the container. In this case, the /usr/games/fortunes is created by the fortune package that it's installed by apt-get install -y fortunes.

Related

Cygwin is not working while creating image in docker

I am new to docker technology.
We are automating build process using make build.
We are creating a image out of dockerfile which should accept shell script(build.sh) as parameter. but it always says build.sh not found.
I tried with various versions of commands inside docker still no luck.
Here is my code for dockerfile:
# Install node
RUN apk update && apk add
RUN apk add --update nodejs nodejs-npm
RUN apk add --update curl
RUN mkdir /build
COPY ./build.sh /build
RUN chmod +x /build/build.sh
WORKDIR /build
CMD build/build.sh
I am making use of cygwin since I am using windows OS.

docker entrypoint running bash script gets "permission denied"

I'm trying to dockerize my node.js app. When the container is built I want it to run a git clone and then start the node server. Therefore I put these operations in a .sh script. And run the script as a single command in the ENTRYPOINT:
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y build-essential libssl-dev gcc curl npm git
#install gcc 4.9
RUN apt-get install -y software-properties-common python-software-properties
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
RUN apt-get update
RUN apt-get install -y libstdc++-4.9-dev
#install newst nodejs
RUN curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
RUN apt-get install -y nodejs
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD package.json /usr/src/app/
RUN npm install
ADD docker-entrypoint.sh /usr/src/app/
EXPOSE 8080
ENTRYPOINT ["/usr/src/app/docker-entrypoint.sh"]
My docker-entrypoint.sh looks like this:
git clone git#<repo>.git
git add remote upstream git#<upstream_repo>.git
/usr/bin/node server.js
After building this image and run:
docker run --env NODE_ENV=development -p 8080:8080 -t -i <image>
I'm getting:
docker: Error response from daemon: oci runtime error: exec: "/usr/src/app/docker-entrypoint.sh": permission denied.
I shell into the container and the permission of docker-entrypoint.sh is:
-rw-r--r-- 1 root root 292 Aug 10 18:41 docker-entrypoint.sh
three questions:
Does my bash script have wrong syntax?
How do I change the permission of a bash file before adding it into an image?
What's the best way to run multiple git commands in entrypoint without using a bash script?
Thanks.
"Permission denied" prevents your script from being invoked at all. Thus, the only syntax that could be possibly pertinent is that of the first line (the "shebang"), which should look like #!/usr/bin/env bash, or #!/bin/bash, or similar depending on your target's filesystem layout.
Most likely the filesystem permissions not being set to allow execute. It's also possible that the shebang references something that isn't executable, but this is far less likely.
Mooted by the ease of repairing the prior issues.
The simple reading of
docker: Error response from daemon: oci runtime error: exec: "/usr/src/app/docker-entrypoint.sh": permission denied.
...is that the script isn't marked executable.
RUN ["chmod", "+x", "/usr/src/app/docker-entrypoint.sh"]
will address this within the container. Alternately, you can ensure that the local copy referenced by the Dockerfile is executable, and then use COPY (which is explicitly documented to retain metadata).
An executable file needs to have permissions for execute set before you can execute it.
In your machine where you are building the docker image (not inside the docker image itself) try running:
ls -la path/to/directory
The first column of the output for your executable (in this case docker-entrypoint.sh) should have the executable bits set something like:
-rwxrwxr-x
If not then try:
chmod +x docker-entrypoint.sh
and then build your docker image again.
Docker uses it's own file system but it copies everything over (including permissions bits) from the source directories.
I faced same issue & it resolved by
ENTRYPOINT ["sh", "/docker-entrypoint.sh"]
For the Dockerfile in the original question it should be like:
ENTRYPOINT ["sh", "/usr/src/app/docker-entrypoint.sh"]
The problem is due to original file not having execute permission.
Check original file has permission.
run ls -al
If result get -rw-r--r-- ,
run
chmod +x docker-entrypoint.sh
before docker build!
Remove Dot [.]
This problem take with me more than 3 hours finally, I just tried the problem was in removing dot from the end just.
problem was
docker run -p 3000:80 --rm --name test-con test-app .
/usr/local/bin/docker-entrypoint.sh: 8: exec: .: Permission denied
just remove dot from the end of your command line :
docker run -p 3000:80 --rm --name test-con test-app
Grant execution rights to the file docker-entrypoint.sh
sudo chmod 775 docker-entrypoint.sh
This is a bit stupid maybe but the error message I got was Permission denied and it sent me spiralling down in a very wrong direction to attempt to solve it. (Here for example)
I haven't even added any bash script myself, I think one is added by nodejs image which I use.
FROM node:14.9.0
I was wrongly running to expose/connect the port on my local:
docker run -p 80:80 [name] . # this is wrong!
which gives
/usr/local/bin/docker-entrypoint.sh: 8: exec: .: Permission denied
But you shouldn't even have a dot in the end, it was added to documentation of another projects docker image by misstake. You should simply run:
docker run -p 80:80 [name]
I like Docker a lot but it's sad it has so many gotchas like this and not always very clear error messages...
This is an old question asked two years prior to my answer, I am going to post what worked for me anyways.
In my working directory I have two files: Dockerfile & provision.sh
Dockerfile:
FROM centos:6.8
# put the script in the /root directory of the container
COPY provision.sh /root
# execute the script inside the container
RUN /root/provision.sh
EXPOSE 80
# Default command
CMD ["/bin/bash"]
provision.sh:
#!/usr/bin/env bash
yum upgrade
I was able to make the file in the docker container executable by setting the file outside the container as executable chmod 700 provision.sh then running docker build . .
If you do not use DockerFile, you can simply add permission as command line argument of the bash:
docker run -t <image> /bin/bash -c "chmod +x /usr/src/app/docker-entrypoint.sh; /usr/src/app/docker-entrypoint.sh"
If you still get Permission denied errors when you try to run your script in the docker's entrypoint, just try DO NOT use the shell form of the entrypoint:
Instead of:
ENTRYPOINT ./bin/watcher write ENTRYPOINT ["./bin/watcher"]:
https://docs.docker.com/engine/reference/builder/#entrypoint

Dockerfile, how to create images ubuntu 14.04

Yesterday I've been asked about how to make a docker images with dockerfile
This time I want to add a question
If I want to make the OS ubuntu 14:04 on images docker, which it is installed, postgresql-9.3.10, install Java JDK 6, copy the file (significant location), and create a user on the images.
Whether I can combine of several dockerfile as needed for images? (dockerfile of postgresql, java, copyfile, and create a user so one dockerfile)
Example. I made one dockerfile "ubuntu"
which contains the command
top line
# Create dockerfile
# get OS ubuntu to images
FROM ubuntu: 14:04
# !!further adding a command on the following link, below the line per-dockerfile(intends command in dockerfile on the link)
# command on dockerfile postgresql-9.3
https://github.com/docker-library/postgres/blob/ed23320582f4ec5b0e5e35c99d98966dacbc6ed8/9.3/Dockerfile
# command on dockerfile java
https://github.com/docker-library/java/blob/master/openjdk-6-jdk/Dockerfile
# create a user on images ubuntu
RUN adduser myuser
# copy file/directory on images ubuntu
COPY /home/myuser/test /home/userimagedockerubuntu/test
# ?
CMD ["ubuntu:14.04"]
Please help me
No, you cannot combine multiple Dockerfile.
The best practice is to:
start from an imabe already included what you need, like this postgresql image already based on ubuntu.
That means that if your Dockerfile starts with:
FROM orchardup/postgresql
You would be building an image which already contains ubuntu and postgresql.
COPY or RUN what you need in your dockerfile, like for openjdk6:
RUN \
apt-get update && \
apt-get install -y openjdk-6-jdk && \
rm -rf /var/lib/apt/lists/*
ENV JAVA_HOME /usr/lib/jvm/java-6-openjdk-amd64
Finally, your default command should run the service you want:
# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
But since the Dockerfile of orchardup/postgresql already contains a CMD, you don't even have to specify one: you will inherit from the CMD defined in your base image.
I think nesting multiple Dockerfiles is not possible due to the layer system. You may however outsource tasks into shell scripts and run those in your Dockerfile.
In your Dockerfile please fix the base image:
FROM ubuntu:14.04
Further your CMD is invalid. You may want to execute a bash with CMD ["bash"] that you can work with.
I would suggest you to start with the doc on Dockerfile as you clearly missed this and it contains all the answers to your questions, and even questions you don't even think to ask yet.

ADD Command for Docker Toolbox on Mac

I have Docker Toolbox installed on my Mac, but I'm having issues adding a file to a container during build. I'm using the ADD command in the Dockerfile. I can't seem to add any local files. I understand that Docker Toolbox uses VirtualBox under the hood, but I am not sure how to get those files into the VM to build the container. Is there a way I can do it that allows me to keep a clean OS-agnostic Dockerfile without any absolute paths?
Here is my Dockerfile. It's built from the Node.js container with some additional dependencies.
FROM node:4.2.2
RUN apt-get update
RUN apt-get install -y libvips-dev libgsf-1-dev libkrb5-dev
RUN apt-get clean
ADD app/ /app
RUN cd /app && npm install --production
RUN npm install forever -g
Turns out this does work, but only for my current directory. My Dockerfile and the files I wanted to add were not in the same directory. Moving the shell to the files I wanted, and then manually specifying the Dockerfile worked.
docker build -f my/other/Dockerfile .
Since docker will use a VirtualBox VM on Mac (with boot2docker or with docker-machine), it will use VirtualBox Guest Additions, which is there for the express purpose of using VirtualBox folder sharing.
Make sure to be in such a shared path, typically in /Users/....
If app/ is in /Users/path/to/app, then ADD should work.
You can mount other paths with boot2docker, but it can be problematic with docker machine (see issue 13).
Of course, for ADD app ... to work, you need to be in the parent folder of app/.
From docker ADD:
The <src> path must be inside the context of the build; you cannot ADD ../something /something

Docker - container with multiple images

I wanted to make a Dockerfile with multiple images to run in one container.
What is the best method to go about this? Below is a list of what I wanted to run in a single container. I have not have any luck with making a Dockerfile with all of these included.
MySQL Server
RabbitMQ
Java8
Node.js
Xvfb
Firefox
Chrome
This is what I have so far, can I get a few tips
FROM stackbrew/ubuntu:12.04
MAINTAINER
# Update the repository sources list #RUN apt-get update
# My SQL Server ###############
RUN apt-get
update -qq && apt-get
install -y mysql-server-5.5
ADD my.cnf /etc/mysql/conf.d/my.cnf
RUN chmod 664 /etc/mysql/conf.d/my.cnf
ADD run /usr/local/bin/run
RUN chmod +x /usr/local/bin/run V
OLUME ["/var/lib/mysql"]
EXPOSE 3306
CMD ["/usr/local/bin/run"]
You cannot have "multiple images to run in one container", that wouldn't make sense.
But you can write a Dockerfile to create an image that will install all the services you mentionned. Example (Ubuntu/Debian distribution) :
[...header...]
FROM stackbrew/ubuntu:12.04 #or use ubuntu-upstart:12.04
MAINTAINER BPetkov
# Update the repository sources list
RUN apt-get update -qq
# Mysql
RUN apt-get install -y mysql-server-5.5
ADD my.cnf /etc/mysql/conf.d/my.cnf
RUN chmod 664 /etc/mysql/conf.d/my.cnf
ADD run /usr/local/bin/run
RUN chmod +x /usr/local/bin/run
# Other stuff
RUN apt-get -y install rabbitmq
RUN apt-get -y install nodejs
[...]
VOLUME ["/var/lib/mysql"]
EXPOSE 3306
EXPOSE .......
CMD ["/sbin/init"]
Then you would have to get all of them started automatically when the container starts.
You can use a process manager such as supervisord (Docker documentation here).
Alternatively, you could use a regular init system, check this base image : ubuntu-upstart. This one would allow you to only have to install the packages in your Dockerfile and get them started automatically without any effort, by specifying /sbin/init as EntryPoint or CMD in your Dockerfile.
The feature you're looking for is Docker Compose.

Resources