Using --user-data with doctl

I am trying to understand the --user-data flag in the doctl computer docker create command. It’s referenced here:

I thought that something like this might work.

doctl compute droplet create \
    --verbose \
    --wait \
    --image docker-20-04 \
    --size c-2 \
    --region sfo3 \
    --user-data "#!/bin/bash\ndocker pull hello-world;" \

But it doesn’t seem to work.

However, if I make a file (e.g. “”) with contents

docker pull hello-world

And then execute

doctl compute droplet create \
    --verbose \
    --wait \
    --image docker-20-04 \
    --size c-2 \
    --region sfo3 \

It seems to work.

I can’t really find much help on this topic but I appreciate any help I can get. I’d really like to understand the --user-data flag. I’ve tried variations (e.g. not including the #!/bin/bash\n) but haven’t found anything that works yet.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 30, 2024
Accepted Answer


From the issue you’re facing, it seems like the problem is with the way the user data script is being passed through the command line. When you provide the script directly in the command line, special characters like newline (\n) are treated differently than when you provide them in a file. This might be causing your script to not execute as intended.

Here’s a bit more insight and a suggestion:

  1. When you use the --user-data flag directly in the command line, you need to ensure that the script is formatted correctly. However, complex scripts with multiple lines and special characters (like newline \n) can be tricky to pass in correctly due to shell interpretation.

  2. The approach that worked for you involves creating a script file and then using the --user-data-file flag. This is generally more reliable for complex scripts because it avoids issues with shell interpretation and allows you to format the script in a clear, readable manner.

  3. The error message from /var/log/cloud-init-output.log suggests that the script provided through --user-data was not correctly interpreted as a valid script. The ‘text/x-not-multipart’ part indicates that cloud-init did not recognize the format of your user data.

I would personally suggest that, for complex scripts or when you need to include special characters, it’s usually better to use the --user-data-file approach. This ensures your script is read and executed exactly as you’ve written it, without any shell interpretation issues.

However, if you still want to use the --user-data flag directly in the command line for simpler scripts, you need to pay close attention to how special characters are interpreted and make sure the script is correctly formatted as a single line. For example:

doctl compute droplet create --verbose --wait --image docker-20-04 --size c-2 --region sfo3 --user-data $'#!/bin/bash\ndocker pull hello-world;' alpha-engine

In this revised command, the $ before the single quotes is used to tell the shell to interpret escape sequences like \n.

Basically, the key to using --user-data effectively is understanding how your shell interprets and formats the script you’re passing in. For more complex tasks, sticking to --user-data-file is a more reliable and clearer approach.

If you have more questions, feel free to ask!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel